package demo

import (
	"encoding/json"
	"fmt"
)



func AdvancedSelect() {
	Init()
	// DB.AutoMigrate(&Advanced{})
	// Advanced := []Advanced{
	// 	{Id: 1, Name: "李元芳1", Age: 32, Email: PtrString("123321@qq.com"), Gender: true},
	// 	{Id: 2, Name: "李元芳2", Age: 18, Email: PtrString("123456@qq.com"), Gender: true},
	// 	{Id: 3, Name: "李元芳3", Age: 32, Email: PtrString("123789@163.com"), Gender: false},
	// 	{Id: 4, Name: "李元芳4", Age: 32, Email: PtrString("1233212@qq.com"), Gender: true},
	// 	{Id: 5, Name: "李元芳5", Age: 32, Email: PtrString("12332131@qq.com"), Gender: false},
	// 	{Id: 6, Name: "李元芳6", Age: 32, Email: PtrString("1235646@qq.com"), Gender: true},
	// 	{Id: 7, Name: "李元芳7", Age: 32, Email: PtrString("123423432@qq.com"), Gender: true},
	// 	{Id: 8, Name: "李元芳8", Age: 32, Email: PtrString("1236765@qq.com"), Gender: false},
	// 	{Id: 9, Name: "李元芳9", Age: 32, Email: PtrString("123989@qq.com"), Gender: true},
	// 	{Id: 10, Name: "李元芳10", Age: 32, Email: PtrString("123432@qq.com"), Gender: true},
	// 	{Id: 11, Name: "李元芳11", Age: 32, Email: PtrString("12343242@qq.com"), Gender: true},
	// }
	// DB.Create(&Advanced)

	var Advanceds []*Advanced
	//查询名字为李元芳1的数据
	// if err := DB.Where("name = ?", "李元芳1").Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//查询名字不是李元芳1的数据
	// if err := DB.Not("name = ?", "李元芳1").Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//查询名字包含 如燕，马自达的数据
	// if err := DB.Where("name in ?", []string{"如燕", "马自达"}).Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//查询名字姓马的
	// if err := DB.Where("name like ?", "马%").Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//查询年龄小于18并且是用163邮箱的
	// if err := DB.Where("age < ? and email like ?", 18, "%163.com").Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//查询性别为女或者邮箱是163的
	// if err := DB.Where("gender = ?", false).Or("email like ?", "%163.com").Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//指定哪张表查询
	// DB.Table("tableName")
	//根据结构映射的表进行查询，也是指定了哪张表进行查询
	//DB.Model(&Advanced)

	//使用结构体查询 会过滤零值
	// if err := DB.Debug().Where(&Advanced{Name: "李元芳1", Age: 0}).Find(&Advanceds).Error; err != nil {
	// 	panic(err)
	// }

	//选择部分字段查询并且存入到另外一个结构体中
	// type Advanced2 struct {
	// 	Title string `gorm:"column:name"`
	// 	Age  int
	// }
	// var Advanceds2 []Advanced2
	// DB.Debug().Select("name,age").Where(&Advanced{Name: "李元芳1"}).Find(&Advanceds).Scan(&Advanceds2)
	json, _ := json.Marshal(Advanceds)
	fmt.Println(string(json))
}

func PtrString(email string) *string { return &email }
